package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report_lq.domain.LqydjlbBaDetail;
import com.ruoyi.report_lq.domain.LqzrdjlbBaDetail;
import com.ruoyi.report_lq.domain.ReportLqBmjrsyjcjlbBa3;
import com.ruoyi.report_lq.domain.ReportLqYcjldnfxjlbBaDetail;
import com.ruoyi.report_lq.domain.bo.ReportLqBmjrsyjcjlbBa3Bo;
import com.ruoyi.report_lq.domain.bo.ReportLqYcjldnfxjlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.LqydjlbBaDetailVo;
import com.ruoyi.report_lq.domain.vo.LqzrdjlbBaDetailVo;
import com.ruoyi.report_lq.domain.vo.ReportLqBmjrsyjcjlbBa3Vo;
import com.ruoyi.report_lq.domain.vo.ReportLqYcjldnfxjlbBaDetailVo;
import com.ruoyi.report_lq.mapper.ReportLqBmjrsyjcjlbBa3Mapper;
import com.ruoyi.report_lq.mapper.ReportLqYcjldnfxjlbBaDetailMapper;
import com.ruoyi.report_lq.service.IReportLqBmjrsyjcjlbBa3Service;
import com.ruoyi.report_lq.service.IReportLqYcjldnfxjlbBaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class ReportLqYcjldnfxjlbBaServiceImpl implements IReportLqYcjldnfxjlbBaService {

    private final ReportLqYcjldnfxjlbBaDetailMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ReportLqYcjldnfxjlbBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }


    /**
     *
     * @param infoId
     * @return 黏附性集合
     * @method 根据info-id 查询黏附性子表的list集合数据
     */
    public List<ReportLqYcjldnfxjlbBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportLqYcjldnfxjlbBaDetail> query = new QueryWrapper<>();
        query.eq("bsinfo_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportLqYcjldnfxjlbBaDetailVo> queryPageList(ReportLqYcjldnfxjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqYcjldnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqYcjldnfxjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportLqYcjldnfxjlbBaDetailVo> queryList(ReportLqYcjldnfxjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportLqYcjldnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqYcjldnfxjlbBaDetail> buildQueryWrapper(ReportLqYcjldnfxjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqYcjldnfxjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getJillj() != null, ReportLqYcjldnfxjlbBaDetail::getJillj, bo.getJillj());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportLqYcjldnfxjlbBaDetailBo bo) {
        ReportLqYcjldnfxjlbBaDetail add = BeanUtil.toBean(bo, ReportLqYcjldnfxjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportLqYcjldnfxjlbBaDetailBo bo) {
        ReportLqYcjldnfxjlbBaDetail update = BeanUtil.toBean(bo, ReportLqYcjldnfxjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量新增沥青与粗集料黏附性记录子表
     */
    public Boolean insertOrUpdateByNddBo(List<ReportLqYcjldnfxjlbBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportLqYcjldnfxjlbBaDetail detail = new ReportLqYcjldnfxjlbBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setBsinfoId(pId);
            detail.setJillj(detailVo.getJillj());
            detail.setJilwd(detailVo.getJilwd());
            detail.setLiqwd(detailVo.getLiqwd());
            detail.setJilzlqzsj(detailVo.getJilzlqzsj());
            detail.setGuoflqdjllqsj(detailVo.getGuoflqdjllqsj());
            detail.setJinzsj(detailVo.getJinzsj());
            detail.setShiyrygcjg1(detailVo.getShiyrygcjg1());
            detail.setShiyrygcjg2(detailVo.getShiyrygcjg2());
            detail.setPingjnfxdj(detailVo.getPingjnfxdj());
            detail.setZonghpd(detailVo.getZonghpd());
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqYcjldnfxjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
